<template>
  <div class="login-wrap">
    <div class="login-container">
      <div class="login-form">
        <div class="logo-wrapper">
          <div class="icon-container">
            <i class="el-icon-reading animated-icon"></i>
          </div>
          <h1 class="form-title">学生选课管理系统</h1>
          <p class="form-subtitle">教学服务 · 便捷规范 · 高效管理</p>
        </div>
        <div class="form-divider"></div>
        <el-form
          :model="formData"
          :rules="rules"
          class="form-content"
          label-width="0px"
          ref="form"
          :hide-required-asterisk="true"
        >
          <el-form-item prop="username">
            <el-input 
              placeholder="请输入学号/工号/用户名" 
              v-model="formData.username"
              prefix-icon="el-icon-user"
              size="medium"
              class="custom-input"
            >
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              @keyup.enter.native="submit()"
              placeholder="请输入密码"
              type="password"
              v-model="formData.password"
              prefix-icon="el-icon-lock"
              size="medium"
              class="custom-input"
            >
            </el-input>
          </el-form-item>

          <el-form-item prop="userType" class="user-type-select">
            <div class="role-selector">
              <div 
                class="role-option" 
                :class="{'active': formData.userType === '1'}"
                @click="formData.userType = '1'"
              >
                <i class="el-icon-user"></i>
                <span>学生</span>
              </div>
              <div 
                class="role-option" 
                :class="{'active': formData.userType === '2'}"
                @click="formData.userType = '2'"
              >
                <i class="el-icon-s-custom"></i>
                <span>教师</span>
              </div>
              <div 
                class="role-option" 
                :class="{'active': formData.userType === '3'}"
                @click="formData.userType = '3'"
              >
                <i class="el-icon-s-tools"></i>
                <span>教务管理员</span>
              </div>
            </div>
          </el-form-item>

          <div class="login-btn" v-loading="this.$store.state.loading">
            <el-button @click="submit()" type="primary" size="medium" class="submit-btn">
              <i class="el-icon-right login-icon"></i> 登录
            </el-button>
          </div>
        </el-form>
        <div class="copyright">
          <i class="el-icon-info"></i> © 2024 学生选课管理系统 - 版权所有
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";

export default {
  data: function() {
    return {
      formData: {
        username: "",
        password: "",
        userType: "1"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        userType: [
          { required: true, message: "请选择用户类型", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          login(
            this.formData.username,
            this.formData.password,
            this.formData.userType
          ).then(res => {
            this.$message.success("登录成功: " + res.username);
            this.$store.commit("login", res);
            this.$router.push({ name: "container" });
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../assets/login-background.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(5px);
}

.login-form {
  width: 420px;
  padding: 30px 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  position: relative;
  transition: all 0.3s ease;
  animation: fadeInUp 0.6s ease-out;
}

.logo-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.icon-container {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, #42d392, #41b883);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15px;
  box-shadow: 0 5px 15px rgba(65, 184, 131, 0.3);
  animation: pulse 2s infinite;
}

.animated-icon {
  font-size: 40px;
  color: white;
  animation: bounce 2s infinite;
}

.form-title {
  width: 100%;
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 5px 0;
  animation: fadeIn 0.8s ease-out;
}

.form-subtitle {
  color: #606266;
  font-size: 14px;
  text-align: center;
  margin: 0;
  animation: fadeIn 1s ease-out;
}

.form-divider {
  height: 1px;
  background: linear-gradient(to right, transparent, #dcdfe6, transparent);
  margin: 15px 0;
  position: relative;
  overflow: hidden;
}

.form-divider::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(to right, transparent, rgba(65, 184, 131, 0.5), transparent);
  animation: shimmer 3s infinite;
}

.form-content {
  padding: 10px 0;
}

.custom-input >>> .el-input__inner {
  height: 44px;
  line-height: 44px;
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  padding-left: 45px;
  font-size: 14px;
  transition: all 0.3s;
}

.custom-input >>> .el-input__inner:focus {
  border-color: #41b883;
  box-shadow: 0 0 0 2px rgba(65, 184, 131, 0.2);
}

.custom-input >>> .el-input__prefix {
  left: 15px;
}

.custom-input >>> .el-input__icon {
  font-size: 18px;
  line-height: 44px;
  color: #41b883;
}

.user-type-select {
  margin-top: 10px;
}

.role-selector {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 44px;
  border-radius: 8px;
  background-color: #f5f7fa;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.role-option {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  height: 100%;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #606266;
  position: relative;
}

.role-option i {
  margin-right: 5px;
  font-size: 16px;
}

.role-option:hover {
  background-color: rgba(65, 184, 131, 0.1);
}

.role-option.active {
  background-color: #41b883;
  color: white;
}

.role-option.active::before {
  content: '';
  position: absolute;
  top: 6px;
  left: 6px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: white;
}

.login-btn {
  margin-top: 25px;
  text-align: center;
}

.submit-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  border-radius: 8px;
  background: linear-gradient(135deg, #42d392, #41b883);
  border: none;
  transition: all 0.3s;
  box-shadow: 0 5px 15px rgba(65, 184, 131, 0.3);
  overflow: hidden;
  position: relative;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(65, 184, 131, 0.4);
}

.submit-btn:hover::before {
  transform: scaleX(1.5);
  opacity: 0;
}

.submit-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.1);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.5s, opacity 0.5s;
  z-index: 0;
}

.login-icon {
  margin-right: 4px;
  animation: slideRight 1s infinite alternate;
}

.copyright {
  width: 100%;
  text-align: center;
  font-size: 12px;
  color: #909399;
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.copyright i {
  margin-right: 4px;
  font-size: 14px;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(65, 184, 131, 0.4);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(65, 184, 131, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(65, 184, 131, 0);
  }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

@keyframes shimmer {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

@keyframes slideRight {
  from {
    transform: translateX(-3px);
  }
  to {
    transform: translateX(3px);
  }
}

@media screen and (max-width: 768px) {
  .login-form {
    width: 85%;
    padding: 25px;
  }
}
</style>
